<?php

/**
 * InsAbonnement form base class.
 *
 * @method InsAbonnement getObject() Returns the current form's model object
 *
 * @package    tfe
 * @subpackage form
 * @author     3emeIG
 * @version    SVN: $Id: sfDoctrineFormGeneratedInheritanceTemplate.php 24171 2009-11-19 16:37:50Z Kris.Wallsmith $
 */
abstract class BaseInsAbonnementForm extends InscriptionForm
{
  protected function setupInheritance()
  {
    parent::setupInheritance();

    $this->widgetSchema   ['id_abonnement'] = new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Abonnement'), 'add_empty' => false));
    $this->validatorSchema['id_abonnement'] = new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Abonnement')));

    $this->widgetSchema   ['jour_de_la_semaine'] = new sfWidgetFormChoice(array('choices' => array('lundi' => 'lundi', 'mardi' => 'mardi', 'mercredi' => 'mercredi', 'jeudi' => 'jeudi', 'vendredi' => 'vendredi', 'samedi' => 'samedi', 'dimanche' => 'dimanche')));
    $this->validatorSchema['jour_de_la_semaine'] = new sfValidatorChoice(array('choices' => array(0 => 'lundi', 1 => 'mardi', 2 => 'mercredi', 3 => 'jeudi', 4 => 'vendredi', 5 => 'samedi', 6 => 'dimanche'), 'required' => false));

    $this->widgetSchema   ['parrain'] = new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Parrain'), 'add_empty' => true));
    $this->validatorSchema['parrain'] = new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Parrain'), 'required' => false));

    $this->widgetSchema   ['cadeau_recu'] = new sfWidgetFormInputCheckbox();
    $this->validatorSchema['cadeau_recu'] = new sfValidatorBoolean(array('required' => false));

    $this->widgetSchema   ['paiement_en_deux_fois'] = new sfWidgetFormChoice(array('choices' => array('non' => 'non', 'oui' => 'oui')));
    $this->validatorSchema['paiement_en_deux_fois'] = new sfValidatorChoice(array('choices' => array(0 => 'non', 1 => 'oui'), 'required' => false));

    $this->widgetSchema   ['mode_de_paiement_second_semestre'] = new sfWidgetFormChoice(array('choices' => array('' => '', 'liquide' => 'liquide', 'virement' => 'virement', 'carte' => 'carte', 'chèque' => 'chèque')));
    $this->validatorSchema['mode_de_paiement_second_semestre'] = new sfValidatorChoice(array('choices' => array(0 => '', 1 => 'liquide', 2 => 'virement', 3 => 'carte', 4 => 'chèque'), 'required' => false));

    $this->widgetSchema   ['date_de_paiement_second_semestre'] = new sfWidgetFormDate();
    $this->validatorSchema['date_de_paiement_second_semestre'] = new sfValidatorDate(array('required' => false));

    $this->widgetSchema   ['code_de_paiement_second_semestre'] = new sfWidgetFormInputText();
    $this->validatorSchema['code_de_paiement_second_semestre'] = new sfValidatorString(array('max_length' => 10, 'required' => false));

    $this->widgetSchema   ['a_envoyer'] = new sfWidgetFormInputCheckbox();
    $this->validatorSchema['a_envoyer'] = new sfValidatorBoolean(array('required' => false));

    $this->widgetSchema   ['envoyee'] = new sfWidgetFormInputCheckbox();
    $this->validatorSchema['envoyee'] = new sfValidatorBoolean(array('required' => false));

    $this->widgetSchema   ['liste_des_representations_list'] = new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'RepresentationSpectacle'));
    $this->validatorSchema['liste_des_representations_list'] = new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'RepresentationSpectacle', 'required' => false));

    $this->widgetSchema->setNameFormat('ins_abonnement[%s]');
  }

  public function getModelName()
  {
    return 'InsAbonnement';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['liste_des_representations_list']))
    {
      $this->setDefault('liste_des_representations_list', $this->object->ListeDesRepresentations->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveListeDesRepresentationsList($con);

    parent::doSave($con);
  }

  public function saveListeDesRepresentationsList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['liste_des_representations_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->ListeDesRepresentations->getPrimaryKeys();
    $values = $this->getValue('liste_des_representations_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('ListeDesRepresentations', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('ListeDesRepresentations', array_values($link));
    }
  }

}
